#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:空心竹子

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import requests
import random
import re
import time

class ProcessOn():
    '''
        ProcessOn 半自动注册机(未解决滑动验证码问题)
        使用教程: 创建类时输入邀请地址或者使用 set_url函数设置邀请地址,然后执行 run() 函数即可
    '''  
    # 初始化
    def __init__(self, url):
        self.url = None
        self.browser = None
        self.wait = None
        self.email = None
        self.username = None

        # 登录界面的url
        self.url = url
        # 实例化一个chrome浏览器
        self.browser = webdriver.Chrome()
        # 设置等待超时
        self.wait = WebDriverWait(self.browser, 20)

    def set_url(self, url):
    	self.url = url

    def random_str(self, lenght = 10):
        '''
          随机生成固定长度的字符串,包括大小写字母,数字
        '''
        if lenght <= 0:
            lenght = 10
        parse = 'abcdefghijklnmopqrstuvwxyz1234567890'
        result = ''
        for i in range(0,lenght):
            k = random.randint(0,35)
            result += parse[k:k+1]
        return result

    def check_verification_email(self, email):
        '''
            检测是否收到验证邮件,未收到返回 None
        '''
        # print(email)
        url = 'https://mailsac.com/api/addresses/' + email + '/messages'
        res = requests.get(url)
        data = res.json()
        # print(data)
        for d in data:
            if d['from'][0]['name'] == 'ProcessOn':
                return d['_id']
        return None

    def verification_email(self, email, id):
        url = 'https://mailsac.com/api/text/' + email + '/' + id
        res = requests.get(url)
        text = res.text
        text = text.replace('\n', '')
        ss = re.findall('verification/(.+?)\[',text)
        url = 'https://www.processon.com/signup/verification/' + ss[0]
        res = requests.get(url)
        return res.status_code

    def register(self):
        self.browser.get(self.url)
        register_button = self.browser.find_element_by_xpath('/html/body/header/ul/li[6]/a')
        register_button.click()

        input_email = self.browser.find_element_by_id('login_email')
        input_email.send_keys(self.email)

        input_password = self.browser.find_element_by_id('login_password')
        input_password.send_keys(self.username)

        input_fullname = self.browser.find_element_by_id('login_fullname')
        input_fullname.send_keys(self.username)

        signup_btn = self.browser.find_element_by_id('signup_btn')

        while True:
            try:
                isSigup = signup_btn.get_attribute('display')
                print('请进行图形码验证...')
                time.sleep(1)
            except Exception as e:
                break
        submit_url = 'https://www.processon.com/signup/submit'
        if submit_url == self.browser.current_url:
            return True
        return False

    def run(self):
        i = 0
        while 1 < 100:
            i = i+1
            self.username = self.random_str()
            self.email = self.username + '@mailsac.com'

            print('正在注册第%s个账号...' % i)
            print('\t生成随机邮箱 %s' % self.email)
            if self.register() == True:
                k = 0
                while True:
                    print('\t正在等待邀请邮件%d...' % k)
                    id = self.check_verification_email(self.email)
                    if id == None:
                        k = k + 1
                        if k > 100:
                            print('\t等待时间过长,自动跳过...')
                            break
                        time.sleep(1)
                    else:
                        print('\t已接收到邮件,准备验证...')
                        code = self.verification_email(self.email, id)
                        if code == 200:
                            print('\t验证成功')
                        else:
                            print('\t验证失败')
                        break

# 个人邀请码,将你的邀请码复制到这里
code = ''

url = 'https://www.processon.com/i/' + code
processon = ProcessOn(url = url)
processon.run()
